package com.guohengspace.mq.utils;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author zhangfei
 * @date 2023/3/27 16:19
 */

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public  static <T> T getBean(String beanName){
        return (T) context.getBean(beanName);
    }

    public  static <T> T getBean(String beanName,Class<T> tClass){
        return context.getBean(beanName,tClass);
    }

    public  static <T> T getBean(Class<T> tClass){
        return context.getBean(tClass);
    }

    public static String getProperty(String key){
        return  context.getEnvironment().getProperty(key);
    }

}
